/*
 * Copyright (c) 2008, Solido Systems
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of Solido Systems nor the names of its contributors may be
 * used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.solidosystems.objectkitchen.index;

import java.io.*;

public class IndexEntry{
	private long location;
	private long targetOffset;
	private int targetFileId;
	private int hashCode;
	private byte[] id;
	private boolean deleted;
	
	public IndexEntry(long location,long targetOffset, int targetFileId, int hashCode, byte[] id){
		this.location=location;
		this.targetOffset=targetOffset;
		this.targetFileId=targetFileId;
		this.hashCode=hashCode;
		this.id=id;
		this.deleted=false;
	}
	
	public void compareToEntry(IndexEntry entry){
		if(location!=entry.location)System.out.println("Location differs");
		if(targetOffset!=entry.targetOffset)System.out.println("targetOffset differs");
		if(targetFileId!=entry.targetFileId)System.out.println("targetFileId differs");
		if(hashCode!=entry.hashCode)System.out.println("hashCode differs");
	}
	
	public boolean isDeleted(){
		return deleted;
	}
	
	public void setDeleted(boolean value){
		this.deleted=value;
	}
	
	public int getHashCode(){
		return hashCode;
	}
	
	public String getId(){
		try{
			return new String(id,"UTF-8");
		}catch(Exception e){}
		return "encoding error";
	}
	
	public long getLocation(){
		return location;
	}
	
	public int getFileId(){
		return targetFileId;
	}
	
	public long getFileOffset(){
		return targetOffset;
	}
	
	public void setFileOffset(long offset){
		targetOffset=offset;
	}
	
	public boolean matchesId(byte[] buf){
		if(buf.length!=id.length)return false;
		for(int i=0;i<buf.length;i++){
			if(id[i]!=buf[i])return false;
		}
		return true;
	}
	
	public void writeToStream(DataOutputStream file) throws IOException{
		file.writeInt(hashCode);
		file.writeInt(id.length);
		file.write(id);
		file.writeInt(targetFileId);
		file.writeLong(targetOffset);
		file.writeBoolean(deleted);
		file.flush();
	}
	
	public void writeToFile(RandomAccessFile file) throws IOException{
		file.writeInt(hashCode);
		file.writeInt(id.length);
		file.write(id);
		file.writeInt(targetFileId);
		file.writeLong(targetOffset);
		file.writeBoolean(deleted);
		file.getFD().sync();
	}
	
	public static IndexEntry readFromFile(RandomAccessFile file) throws IOException{
		long location=file.getFilePointer();
		int hashCode=file.readInt();
		int size=file.readInt();
		byte[] id=new byte[size];
		file.readFully(id);
		int targetFileId=file.readInt();
		long targetLocation=file.readLong();
		boolean deleted=file.readBoolean();
		IndexEntry entry= new IndexEntry(location,targetLocation,targetFileId,hashCode,id);
		entry.setDeleted(deleted);
		return entry;
	}
}